package com.pinecone.controller;

import java.util.List;
import java.util.Map;


import com.pinecone.service.TxVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pinecone.entity.LiveRecordEntity;
import com.pinecone.service.LiveRecordService;
import com.pinecone.common.utils.PageUtils;
import com.pinecone.common.utils.Query;
import com.pinecone.common.utils.R;




/**
 * 腾讯录制回调记录表
 * 
 * @author hushangjie
 * @email 979783618@qq.com
 * @date 2017-10-14 00:25:28
 */
@RestController
@RequestMapping("live_record")
public class LiveRecordController {
	@Autowired
	private LiveRecordService liveRecordService;
	@Autowired
    private TxVideoService txVideoService;
	/**
	 * 列表
	 */
	@GetMapping("")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		if (params.get("page") != null && params.get("limit")!= null){
			List<LiveRecordEntity> liveRecordList = liveRecordService.queryList(query);
			int total = liveRecordService.queryTotal(query);
			
			PageUtils pageUtil = new PageUtils(liveRecordList, total, query.getLimit(), query.getPage());
			
			return R.ok().put("page", pageUtil);
		}else{
			List<LiveRecordEntity> liveRecordList = liveRecordService.queryList(query);
			return R.ok().put("list", liveRecordList);
		}
		
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/{liveRecordId}")
	public R info(@PathVariable("liveRecordId") Integer liveRecordId){
		LiveRecordEntity liveRecord = liveRecordService.queryObject(liveRecordId);
		
		return R.ok().put("liveRecord", liveRecord);
	}
	
	/**
	 * 保存
	 */
	@PostMapping("")
	public R save(@RequestBody LiveRecordEntity liveRecord){
		liveRecordService.save(liveRecord);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@PutMapping("")
	public R update(@RequestBody LiveRecordEntity liveRecord){
		liveRecordService.update(liveRecord);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@DeleteMapping("")
	public R delete(@RequestBody Integer[] liveRecordIds){

		//删除云点播中的内容
        LiveRecordEntity liveRecordEntity = liveRecordService.queryObject(liveRecordIds[0]);
        txVideoService.deleteVideoById(liveRecordEntity.getFileId());
        //删除本地记录
        liveRecordService.deleteBatch(liveRecordIds);
		return R.ok();
	}
	
}
